layui.use(['upload','table'], function(){
    var $ = layui.jquery
        ,upload = layui.upload;
    var table = layui.table;
    //演示多文件列表
    var demoListView = $('#demoList');
    var uploadListIns = upload.render({
        elem: '#testList'
        ,url: '/layui/uploadFile'
        ,accept: 'file'
        ,multiple: true
        ,number: 3
        ,auto: false
        ,bindAction: '#testListAction'
        ,choose: function(obj){
            debugger;
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列

            //读取本地文件
            obj.preview(function(index, file, result){
                var tr = $(['<tr id="upload-'+ index +'">'
                    ,'<td>'+ file.name +'</td>'
                    ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                    ,'<td>等待上传</td>'
                    ,'<td>'
                    ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                    ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
                    ,'</td>'
                    ,'</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function(){
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function(){
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                demoListView.append(tr);
            });
        }
        ,done: function(res, index, upload){
            if(res.code == '200'){ //上传成功
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html('<a href="'+res.data+'">连接</a>'); //清空操作
                delete this.files[index]; //删除文件队列已经上传成功的文件
                return;
            }
            this.error(index, upload);
        }
        ,allDone: function(obj){
            console.log(obj)
        }
        ,error: function(index, upload){
            var tr = demoListView.find('tr#upload-'+ index)
                ,tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });
    //执行一个 table 实例
    table.render({
        elem: '#fileTable'
        ,height: 420
        ,url: '/layui/getList' //数据接口
        ,title: '用户表'
        ,page: true //开启分页
        ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        ,totalRow: true //开启合计行
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', title: 'ID', sort: true, fixed: 'left', type : 'numbers'}
            ,{field: 'type', title: '文件类型', sort: true,templet :function (d) {
                    var html = '';
                    if (d.type == '0'){
                        html += '<span class="btn btn-success">图片</span>'
                    }else if (d.type == '1') {
                        html += '<span class="btn btn-success">文档</span>'
                    }else if (d.type == '2') {
                        html += '<span class="btn btn-success">视频</span>'
                    }else if (d.type == '3') {
                        html += '<span class="btn btn-success">音频</span>'
                    }else {
                        html += '<span class="btn btn-success">其他</span>'
                    }
                    return html;
                }}
            ,{field: 'url', title: 'URL',width :500, sort: true,templet :function (d) {
                    var html = '';
                    if (d.type == '0'){
                        html += '<a href="'+d.url+'"><img  src="'+d.url+'"></a>'
                    }else {
                        html += '<span class="btn btn-success">'+d.url+'</span>'
                    }
                    return html;
                }}
            ,{field: 'createDate', title: 'createDate', sort: true}
            ,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
        ]]
        ,request: {
            pageName: 'page' //页码的参数名称，默认：page
            ,limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            ,statusCode: 200 //规定成功的状态码，默认：0
            ,msgName: 'msg' //规定状态信息的字段名称，默认：msg
            ,countName: 'count' //规定数据总数的字段名称，默认：count
            ,dataName: 'data' //规定数据列表的字段名称，默认：data
        }
    });

});